package com.itboye.eweilisten.util.network;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.alipay.android.phone.mrpc.core.NetworkUtils;
import com.itboye.eweilisten.me.home.NetWorkActivity;
import com.itboye.eweilisten.util.loadingutil.MAlert;

/**
 * 网络状态广播接收器
 *
 * @author zihao
 *
 */
public class NetState extends BroadcastReceiver {
    public static boolean IS_ENABLE = true;
    private NetworkInfo gprs;
    private NetworkInfo wifi;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 监听网络状态
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        startActivity(context);

    }
    /**
     * 跳转
     *
     * @param context
     */
    private void startActivity(Context context) {
        if (!gprs.isConnected() && !wifi.isConnected() && IS_ENABLE) {
            System.out.println("start");
            IS_ENABLE = false;

            Log.d("errorNetWork","");
//            Intent intent = new Intent().setClass(context,
//                    NetWorkActivity.class);
//            ((Activity) context).startActivityForResult(intent, 1);
        }else{
            MAlert.alert("网络正常");
            context.sendBroadcast(new Intent("connect_recover").putExtra("recover",400));
        }
    }




}